<template>
  <div>
    <a-card class="card-header">
      <a-form-model ref="ruleForm" :model="model" :rules="rules">
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="验收单号"
          prop="acceptanceSheetNum"
          hasFeedback
          style="width: 33%;float: left;"
        >
          <a-input
            placeholder="请输入备料单号"
            v-model.trim="model.acceptanceSheetNum"
          />
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="仓库"
          hasFeedback
          prop="wareHouseId"
          style="width: 33%;float: left;"
        >
          <a-select v-model.trim="model.wareHouseId" placeholder="请选择仓库">
            <a-select-option
              v-for="item in wareHouseList"
              :key="item.id"
              :value="item.id"
            >
              {{ item.wareHouseName }}
            </a-select-option>
          </a-select>
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="加工单号"
          hasFeedback
          prop="processOrderNum"
          style="width: 33%;float: left;"
        >
          <a-input
            placeholder="请输入加工单号"
            v-model.trim="model.processOrderNum"
          />
        </a-form-model-item>
        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="本次验收数量"
          hasFeedback
          prop="acceptanceNumber"
          style="width: 33%;float: left;"
        >
          <a-input-number
            style="width:100%"
            placeholder="请输入本次验收数量"
            id="inputNumber"
            v-model.trim="model.acceptanceNumber"
            :min="1"
          />
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="合格数量"
          hasFeedback
          prop="checkNumber"
          style="width: 33%;float: left;"
        >
          <a-input-number
            style="width:100%"
            placeholder="请输入合格数量"
            id="inputNumber"
            v-model.trim="model.checkNumber"
            :min="1"
          />
        </a-form-model-item>

        <a-form-model-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="工序"
          hasFeedback
          prop="workStageId"
          style="width: 33%;float: left;"
        >
          <a-input placeholder="请输入工序" v-model.trim="model.workStageId" />
        </a-form-model-item>
      </a-form-model>
    </a-card>

    <a-card class="card-content">
      <div
        :style="{
          width: '100%',
          background: '#fff',
        }"
      >
        <a-spin :spinning="confirmLoading" style="width: 100%;">
          <div>
            <a-button
              type="primary"
              style="margin: 0 20px 20px 0;"
              @click="handleAdd"
              >新增用料</a-button
            >
            <!-- <a-button type="primary">EXCEL导入</a-button> -->
            <a-table
              :columns="columns"
              :data-source="tableData"
              :loading="loading"
              :pagination="pagination"
              @change="handleTableChange"
              bordered
              :rowKey="(record, index) => record.id"
            >
              <template slot="action" slot-scope="text, record">
                <a @click="handleEdit(record)">编辑</a>
                <a-divider type="vertical" />
                <a-popconfirm
                  okText="确定"
                  cancelText="取消"
                  v-if="tableData.length"
                  title="确定删除吗?"
                  @confirm="() => handleDelete(record)"
                >
                  <a>删除</a>
                </a-popconfirm>
              </template>
            </a-table>
          </div>
        </a-spin>

        <div style="text-align: center;">
          <a-button style="margin: 20px;" @click="cancel">取消</a-button>
          <a-button type="primary" @click="handleSubmit">保存</a-button>
        </div>
      </div>

      <PickingAddModal
        :goodsList="goodsList"
        ref="modalForm"
        @ok="modalFormOk"
      ></PickingAddModal>
    </a-card>
  </div>
</template>

<script>
import { columns } from "./components/tableConfig";
import { tableMinxins } from "@/unit/mixins/tableMinxins.js";
import PickingAddModal from "./components/PickingAddModal.vue";
import api from "@/api/apiServer";
export default {
  components: { PickingAddModal },
  mixins: [tableMinxins],
  data() {
    return {
      columns,
      tableData: [],
      loading: false,
      pagination: false,
      wareHouseList: [], // 仓库列表
      goodsList: [], // 商品列表

      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },
      confirmLoading: false,
      rules: {
        acceptanceSheetNum: [{ required: true, message: "请输入验收单号!" }],
        processOrderNum: [{ required: true, message: "请输入加工单号!" }],
        acceptanceNumber: [{ required: true, message: "请输入本次验收!" }],
        wareHouseId: [{ required: true, message: "请输入仓库号!" }],
        checkNumber: [{ required: true, message: "请输入合格数量!" }],
        workStageId: [{ required: true, message: "请输入工序!" }],
      },
    };
  },
  mounted() {
    if (this.$route.query.record) {
      this.model = JSON.parse(this.$route.query.record);
      this.model.wareHouseId = Number(this.model.wareHouseId);
      if (this.model.goods) {
        this.tableData = this.model.goods;
      }
    } else {
      this.getNumber();
    }

    this.getWareHouseList();
    this.getGoodsList();
  },
  methods: {
    // 获取备料单号
    async getNumber() {
      const res = await api.ReceiptSheetGetNumber();
      this.model.acceptanceSheetNum = res.data;
    },

    // 获取仓库列表
    async getWareHouseList() {
      var params = {};
      const res = await api.wareHouseList(params);
      this.wareHouseList = res.data;
    },

    // 获取商品列表
    async getGoodsList() {
      var params = {};
      const res = await api.GoodsFindAll(params);
      this.goodsList = res.data;
    },

    // 删除
    async handleDelete(record) {
      var curIndex = 0;
      for (var i = 0; i < this.tableData.length; i++) {
        if (record.goodsCode == this.tableData[i].goodsCode) {
          curIndex = i;
        }
      }
      this.tableData.splice(curIndex, 1);
    },

    // 新增回调
    modalFormOk(data) {
      this.tableData.push(data);
    },

    // 确定
    async handleSubmit() {
      let res;
      this.confirmLoading = true;

      this.model.goods = this.tableData;
      //根据id判断是修改还是新增
      if (this.model.id) {
        res = await api.ReceiptSheetUpdate(this.model); //修改
      } else {
        res = await api.ReceiptSheetInsert(this.model); //新增
      }
      if (res.code && res.code !== 200) {
        this.$message.error(res.message);
        this.confirmLoading = false;
        return;
      }
      this.$message.success(res.message);
      this.confirmLoading = false;
      this.$router.push("/checksheet");
      // this.$emit("ok", this.model);
      // this.visible = false;
    },

    // 取消
    cancel() {
      this.$router.push("/checksheet");
    },
  },
};
</script>

<style lang="less" scoped>
.card-header {
  margin: 20px 0;
}
.card-content {
  button {
    margin-right: 20px;
  }
}
</style>
